package com.example.javapoint.sync.block;

/**
 * 使用匿名内部类同步代码块
 */
class Sender {
    public void senderMsg (String msg) {
        System.out.println("\nSending a Message:" + msg);
        try {
            Thread.sleep(800);
        } catch (InterruptedException e) {
            System.out.println("Thread is interrupted");
        }
        System.out.println("\n" +  msg  + " Sent");
    }
}

class SenderWThreads extends Thread{
    private String msg;
    Sender sender;
    public SenderWThreads(String msg, Sender sender) {
        this.msg = msg;
        this.sender = sender;
    }

    @Override
    public void run() {
        synchronized (sender) {
            sender.senderMsg(msg);
        }
    }
}

public class TestSyncBlockByAnonymous {
    public static void main(String[] args) {
        Sender sender = new Sender();
        SenderWThreads threads1 = new SenderWThreads("点头yes 摇头no", sender);
        SenderWThreads threads2 = new SenderWThreads("来是come 去是go", sender);

        threads1.start();
        threads2.start();
    }
}
